var { pool } = require('../utils/dbUtil')

var moment = require('moment');

// 插入菜单
function insertMenu(obj) {
    let date = new Date()
    let timestamp = date.getTime();
    let time = moment(date).format("YYYY-MM-DD HH:mm:SS")
    let {
        menuName,
        menuType,
        menuIcon,
        menuIconName,
        menuOrder,
        menuRouteAddress,
        menuState,
        menuComponentPath,
        parentMenu
    } = obj
    let [id, aid, bid] = parentMenu
    let parentMenuId, menuId;
    // let menuCreateTime = time
    if (menuType == 0) {
        parentMenuId = `${id}`;
        menuId = `a${timestamp}`
    } else if (menuType == 1) {
        parentMenuId = `${aid}`;
        menuId = `b${timestamp}`
    } else {
        parentMenuId = `${bid}`;
        menuId = `c${timestamp}`
    }
    let sql = `INSERT INTO menu(parentMenuId,menuId,menuName,menuType,menuIcon,menuIconName,menuOrder,menuRouteAddress,menuState,menuComponentPath,menuCreateTime) values(?,?,?,?,?,?,?,?,?,?,STR_TO_DATE(?,'%Y-%m-%d %T'))`;
    return new Promise((resolve, reject) => {
        pool.getConnection(function (connError, connection) {
            if (connError) {
                connection.release()
                reject(connError)
            } else {
                connection.query(sql, [parentMenuId, menuId, menuName, menuType, menuIcon, menuIconName, menuOrder, menuRouteAddress, menuState, menuComponentPath, time], (queryError, rows) => {
                    if (queryError) {
                        connection.release()
                        reject(queryError)
                    } else {
                        connection.release()
                        resolve(rows)
                    }
                })
            }
        })
    })
}

// 查询菜单
function selectMenu() {
    let sql1 = `SELECT * from menu WHERE parentMenuId = '-1'`;
    return new Promise((resolve, reject) => {
        pool.getConnection(function (connError, connection) {
            if (connError) {
                connection.release()
                reject(connError)
            } else {
                connection.query(sql1, (queryError1, rows1) => {
                    if (queryError1) {
                        connection.release()
                        reject(queryError1)
                    } else {
                        let as = rows1
                        let aRt = []
                        as.forEach(a => {
                            let sql2 = `SELECT * from menu WHERE parentMenuId = '${a.menuId}'`
                            connection.query(sql2, (queryError2, rows2) => {
                                if (queryError2) {
                                    connection.release()
                                    reject(queryError2)
                                } else {
                                    let bs = rows2
                                    let bRt = {}
                                    bs.forEach(b => {
                                        let sql3 = `SELECT * from menu WHERE parentMenuId = ${b.menuId}`
                                        connection.query(sql3, (queryError3, rows3) => {
                                            if (queryError3) {
                                                connection.release()
                                                reject(queryError3)
                                            } else {
                                                if(rows3.length) {
                                                    bRt.children.push(rows3)
                                                }
                                            }
                                        })
                                    });
                                    let bTimer = setInterval(() => {
                                        if (bs.length == bRt.length) {
                                            clearInterval(bTimer)
                                            if (bRt.children.length) {
                                                // bRt.menuId = 
                                                aRt.children = bRt
                                            }
                                        }
                                    }, 200);
                                }
                            })
                        });
                        let aTimer = setInterval(() => {
                            if (as.length == aRt.length) {
                                clearInterval(aTimer)
                                let endRt = []
                                endRt[0].children = aRt
                                connection.release()
                                resolve(endRt)
                            }
                        }, 200);
                    }
                })
            }
        })
    })
}

module.exports = {
    insertMenu,
    selectMenu
}